package com.dks.repository.impl;

import java.util.*;

import com.dks.mapper.QsMailMapper;
import com.dks.models.Course;
import com.dks.models.QsMail;
import com.dks.repository.IQsMailRepository;

import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;
import com.dks.util.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class QsMailRepository implements IQsMailRepository {

    private QsMailMapper qsMailMapper;

    @Autowired
    public QsMailRepository(final QsMailMapper qsMailMapperIn) {
        this.qsMailMapper = qsMailMapperIn;
    }

    @Override
    public Integer insert(QsMail qsMail){
        return qsMailMapper.insert(qsMail);
    }

    @Override
    public Integer update(QsMail qsMail){
        return qsMailMapper.update(qsMail);
    }

    @Override
    public Integer delete(Integer id){
        return qsMailMapper.delete(id);
    }

    @Override
    public QsMail select(Integer id){
        return qsMailMapper.select(id);
    }

    @Override
    public Integer cntByMap(Map<String,Object> map){
        return qsMailMapper.cntByMap(map);
    }

    @Override
    public List<Integer> getByMap(Map<String,Object> map){
        return qsMailMapper.getByMap(map);
    }

    @Override
    public List<QsMail> getByMapList(Map<String,Object> map){
        return qsMailMapper.getByMapList(map);
    }

    @Override
    public Integer updateByCode(QsMail qsMail){
        return qsMailMapper.updateByCode(qsMail);
    }

    @Override
    public QsMail selectByBfCode(String bfCode){
        return qsMailMapper.selectByBfCode(bfCode);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取站内信列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getQsMailList(Integer userId, Integer mainTitleStatus, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("mainTitleStatus", mainTitleStatus);
        map.put("recStatus", 0);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<QsMail> courseList = qsMailMapper.getByMapList(map);
        PageInfo<QsMail> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), courseList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }


    @Override
    public ResultModel<Object> getQsMailInfo(Integer userId, Integer mainTitleStatus) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("mainTitleStatus", mainTitleStatus);
        map.put("recStatus", 0);
        List<QsMail> courseList = qsMailMapper.getByMapList(map);
        if(courseList.size()==1){
            QsMail qsMail = new QsMail();
            qsMail.setReadStatus(1);
            qsMail.setId(courseList.get(0).getId());
            int row = qsMailMapper.update(qsMail);
        }
        return Result.makeResult(ResultInfo.SUCCESS, courseList);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取站内信未读数量
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getQsMailUnReadCount(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("readStatus", 0);
        map.put("recStatus", 0);
        List<QsMail> courseList = qsMailMapper.getByMapList(map);
        return Result.makeResult(ResultInfo.SUCCESS, courseList.size());
    }

    @Override
    public ResultModel<Object> readAllQsMail(long userId) {
        QsMail qsMail = new QsMail();
        qsMail.setReadStatus(1);
        qsMail.setUserId(userId);
        int row = qsMailMapper.updateReadAllQsMail(qsMail);
        return Result.makeResult(ResultInfo.SUCCESS, row);
    }
}
